﻿using JNPF.Basics.Logic;
using JNPF.Basics.Models;
using JNPF.Basics.Models.Permission.Dtos.Role;
using JNPF.Utils;
using JNPF.Utils.Controller;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace JNPF.Basics.Controller
{
    /// <summary>
    /// 角色管理
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2017.09.21 
    /// </summary>
    [Route("api/Permission/[controller]")]
    public class RoleController : BaseApiController
    {
        private RoleBll roleBll = new RoleBll();

        #region GET
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="input">参数</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResponseResult> GetList(PageInput input)
        {
            var data = await roleBll.GetList(input);
            var output = new PageResult<RoleListOutput>
            {
                list = data.list.MapToList<RoleListOutput>(),
                pagination = data.pagination
            };
            return Success(output);
        }

        /// <summary>
        /// 获取下拉框
        /// </summary>
        /// <returns></returns>
        [HttpGet("Selector")]
        public async Task<ResponseResult> GetSelector()
        {
            var data = (await roleBll.GetList()).FindAll(t => t.EnabledMark.Equals(1));
            var output = data.MapToList<RoleSelectorOutput>();
            return Success(new { list = output });
        }

        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<ResponseResult> GetInfo(string id)
        {
            var entity = await roleBll.GetInfo(id);
            var output = entity.MapTo<RoleInfoOutput>();
            return Success(output);
        }
        #endregion

        #region POST
        /// <summary>
        /// 新建
        /// </summary>
        /// <param name="input">参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResponseResult> CreateAsync([FromBody] RoleCrInput input)
        {
            if (await roleBll.IsExistByEnCode(input.enCode, null))
            {
                return Error("编码已存在");
            }
            if (await roleBll.IsExistByFullName(input.fullName, null))
            {
                return Error("名称已存在");
            }
            var entity = input.MapTo<RoleEntity>();
            var isOk = await roleBll.Create(entity);
            return isOk > 0 ? Success("新建成功") : Error("新建失败");
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<ResponseResult> Delete(string id)
        {
            var entity = await roleBll.GetInfo(id);
            if (entity != null)
            {
                await roleBll.Delete(entity);
            }
            return Success("删除成功");
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="id">主键</param>
        /// <param name="input">参数</param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public async Task<ResponseResult> Update(string id, [FromBody] RoleUpInput input)
        {
            if (await roleBll.IsExistByEnCode(input.enCode, id))
            {
                return Error("编码已存在");
            }
            if (await roleBll.IsExistByFullName(input.fullName, id))
            {
                return Error("名称已存在");
            }
            var entity = input.MapTo<RoleEntity>();
            var isOk = await roleBll.Update(id, entity);
            return isOk > 0 ? Success("更新成功") : Error("更新失败");
        }

        /// <summary>
        /// 更新状态
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        [HttpPut("{id}/Actions/State")]
        public async Task<ResponseResult> UpdateState(string id)
        {
            var entity = await roleBll.GetInfo(id);
            entity.EnabledMark = entity.EnabledMark == 1 ? 0 : 1;
            var isOk = await roleBll.Update(id, entity);
            return isOk > 0 ? Success("更新成功") : Error("更新失败");
        }
        #endregion
    }
}
